<?php
// 发票管理
namespace Admin\Controller;
use Think\Controller;
class InvoiceController extends Controller
{
	public function __construct($value='')
	{
		$this->isAjaxLogin();
	}
	// 生成发票订单（索取发票）
	public function invoiceAdd($value='')
	{
		$contacts	=	I('post.contacts/s');//联系人
		$phone		=	I('post.phone/s');//电话
		$address	=	I('post.address/s');//地址
		$id_list	=	I('post.id_list/s');//充值记录id
		$adv_id		=	I('post.adv_id/d');//广告主id	 

		$user_info=session('user_info'); 
			 
		$invoice_order=M('invoice_order');//发票订单
		$recharge_record=M('recharge_record');//充值记录
		$advertiser=M('advertiser');//广告主

		$adv_name=$advertiser->where(['id'=>$adv_id])->getField('company_name');

		if (empty($adv_id)) {
			 $result['code']=501;
			 $result['msg']='请选择广告主';	
			 $this->ajaxReturn($result);
		}

		if (empty($id_list)) {
			 $result['code']=503;
			 $result['msg']='请选择充值记录';	
			 $this->ajaxReturn($result);
		}

		$where['id']=['in',$id_list];
		$where['invoice_status']=['neq','2'];

		$sql='select count(distinct(advertiser_id)) as count from wm_recharge_record where id in ('.$id_list.') limit 1';

		$sql1='select distinct(advertiser_id) as adv_id from wm_recharge_record where id in ('.$id_list.') limit 1';

		$count2=M()->query($sql)[0]['count'];

		$adv_id1=M()->query($sql1)[0]['adv_id'];
	 

		$count1=$recharge_record->where($where)->count();
		if ($count2>1) {
			 $result['code']=501;
			 $result['msg']='广告主不相同';	
			 $this->ajaxReturn($result);		
		}

		 
		if ($adv_id!=$adv_id1) {
			 $result['code']=501;
			 $result['msg']='广告主不相同';	
			 $this->ajaxReturn($result);
		}

		if ($count1>0){
			 $result['code']=502;
			 $result['msg']='已开票或开票中,不能重复索取发票';	
			 $this->ajaxReturn($result);			 
		}


		$total_amount=$recharge_record
					->where(['id'=>['in',$id_list]])
					->sum('recharge_amount');



		
		$data['user_id'] 			= 		$user_info['user_id'];//
		$data['order_num'] 			=	 	build_order_no();//订单号时
		$data['user_name']		 	= 		$user_info['user_name'];
		$data['mailing_address']	= 		$address;
		$data['invoice_value']		= 		$total_amount;
		$data['contact_name']		= 		$contacts;
		$data['contact_phone']		= 		$phone;
		$data['adv_id']				= 		$adv_id;
		$data['record_id_list']		= 		$id_list;
		$data['generation_time']	= 		time();
		$data['adv_name']			= 		$adv_name;
		 
		M()->startTrans();
		$res1=$invoice_order->add($data);

		$res2=$recharge_record->where(['id'=>['in',$id_list]])->setField('invoice_status',3);
		if ($res1&&$res2) {
			 M()->commit(); 
			 $result['code']=200;
			 $result['msg']='索取成功,等待财务确认';
		}else{
			 $result['code']=500;
			 $result['msg']='索取失败';
			 M()->rollback(); 	
		}

		$this->ajaxReturn($result);
		 
	}


	//  确认开票
	public function confirmInvoice($value='')
	{
		$id=I('get.id');//订单id

		$invoice_order=M('invoice_order');//订单
		$recharge_record=M('recharge_record');//充值记录
		// 验证广告主开票信息
		$adv_id=$invoice_order->where(['id'=>$id])->getField('adv_id');

		$res1=D('Advertiser')->checkInvoice($adv_id);
		if (!$res1) {
            $result['code']=501;
			$result['msg']='请完善广告主开票信息';
			$this->ajaxReturn($result);
        } 



		$invoice_status=$invoice_order->where(['id'=>$id])->getField('invoice_status');
		 
		if ($invoice_status=='1') {
			$result['code']=501;
			$result['msg']='已开票,不能重复开票';
			$this->ajaxReturn($result);
		}

		$id_list=$invoice_order->where(['id'=>$id])->getField('record_id_list');

		M()->startTrans();
		$res1=$recharge_record->where(['id'=>['in',$id_list]])->setField('invoice_status','1');
		$res2=$invoice_order->where(['id'=>$id])->setField('invoice_status','1');

		if ($res1&&$res2) {
			 M()->commit(); 
			 $result['code']=200;
			 $result['msg']='开票成功';
		}else{
			 $result['code']=500;
			 $result['msg']='开票失败';
			 M()->rollback(); 	
		}

		$this->ajaxReturn($result);

	}

	// 开票发送
	public function invoiceSend($value='')
	{
		$id=I('get.id');
		$invoice_order=M('invoice_order')->field('send_status,invoice_status')->where(['id'=>$id])->find();
	 
		if ($invoice_order['invoice_status']=='0') {
			$result['msg']='未开票不能发送';
			$result['code']=502;
			$this->ajaxReturn($result);	 
		}

		if ($invoice_order['send_status']=='1') {
			$result['msg']='已发送,不能重复发送';	 
			$result['code']=501;
			$this->ajaxReturn($result);	 
		}

		$res=M('invoice_order')->where(['id'=>$id])->setField('send_status','1');
		if ($res) {
			$result['msg']='发送成功';	 
			$result['code']=200;
		}else{
			$result['msg']='发送失败';	 
			$result['code']=500;
		}

		$this->ajaxReturn($result);
	}

	// 查看开票信息
	public function invoiceInfo($value='')
	{
		$adv_id=I('get.adv_id');
		
		$res=D('Advertiser')->invoiceInfo($adv_id,$data); 
		if ($res) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$data;
		}
		// else{
		// 	$result['msg']='查询失败';
		// 	$result['code']=500;
		// }

		$this->ajaxReturn($result);
	}
	// 取消订单 原始
	public function cancelInvoice($value='')
	{
		$id=I('get.id/d');


		$invoice_order=M('invoice_order')->field('order_status,send_status')->where(['id'=>$id])->find();

		if ($invoice_order['send_status']=='1') {
			$result['msg']='已发送不能取消';
			$result['code']=501;
			$this->ajaxReturn($result);
		}

		if ($invoice_order['order_status']=='0') {
			$result['msg']='订单已取消,请勿重复操作';
			$result['code']=501;
			$this->ajaxReturn($result);
		}

		$id_list=M('invoice_order')->where(['id'=>$id])->getField('record_id_list');

		M()->startTrans();

		$res1=M('recharge_record')->where(['id'=>['in',$id_list]])->setField('invoice_status','4');//驳回

		$res2=M('invoice_order')->where(['id'=>$id])->setField('order_status','0');

		if ($res1&&$res2) {
			 M()->commit();
			$result['msg']='取消成功';
			$result['code']=200;
		}else{
			M()->rollback(); 
			$result['msg']='操作失败';
			$result['code']=500;
		}

		$this->ajaxReturn($result);

	}

 
	// 发票订单列表
	public function invoiceOrder($value='')
	{
		$page   			=	I('get.page/d',1);
		$page_size 			=	I('get.page_size/d');

		$adv_id				=	I('get.adv_id/s');
		$invoice_status		=	I('get.invoice_status/s');//发票状态
		$send_status		=	I('get.send_status/s');//发票状态

		$where=[];

		if (C('IS_FALSE_DEL')) {
			$where['is_delete'] = '0';
		}else{
			$where=[];
			 
		} 

        if(!empty($adv_id)&&($adv_id>-1)) {
            $where['advertiser_id'] = $adv_id;
        } 

		if(in_array($invoice_status,['0','1'])) {
            $where['invoice_status'] = $invoice_status;
        } 

 		if(in_array($send_status,['0','1'])) {
            $where['send_status'] = $send_status;
        } 

        $where['order_status']='1';
		$model=M('invoice_order')
			 ->where($where);
		$res=my_page($model,$page,$page_size);
		 
		// if (!empty($res['data'])) {

			$data=array_map(function($val){
				$val['generation_time']=date("Y-m-d H:i:s",$val['generation_time']);
				return $val;
			},$res['data']);
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']['total_page']=$res['count'];
			$result['data']['list']=$data;
			 
		// }else{

		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;
		// 	$result['data']['total_page']=$res['count'];
		// 	$result['data']['list']=$data;
		// }

		$this->ajaxReturn($result);
	}
}